﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using CodeStash.Models.Security;
using CodeStash.Services.Contracts;

namespace CodeStash.Services.Implementation
{
    public class MembershipDataProvider : IMembershipDataProvider
    {
        #region IMembershipDataProvider Members

        public List<DirectLookupUserModel> GetUserDataForEmailAddress(string email)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            List<DirectLookupUserModel> usersThatMatched = new List<DirectLookupUserModel>();

            using (SqlConnection memberShipDBConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetUserDataByEmail", memberShipDBConnection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Email", email));
                    
                    memberShipDBConnection.Open();
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);


                    while (reader.Read())
                    {

                        DirectLookupUserModel user = new DirectLookupUserModel(
                            reader.GetGuid(0),
                            reader.GetString(1),
                            reader.GetString(2),
                            false);

                        usersThatMatched.Add(user);
                    };
                    reader.Close();
                }
            }


            foreach (DirectLookupUserModel userThatMatched in usersThatMatched)
            {
                UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(userThatMatched.UserName);
                userThatMatched.IsOpenIdLoggedInUser = profile.IsOpenIdLoggedInUser;
            }

            return usersThatMatched;
        }

        public DirectLookupUserModel GetUserDataForUserId(Guid userId)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            List<DirectLookupUserModel> usersThatMatched = new List<DirectLookupUserModel>();

            using (SqlConnection memberShipDBConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetUserDataForUserId", memberShipDBConnection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserId", userId));
                    
                    memberShipDBConnection.Open();
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);


                    while (reader.Read())
                    {

                        DirectLookupUserModel user = new DirectLookupUserModel(
                            reader.GetGuid(0),
                            reader.GetString(1),
                            reader.GetString(2),
                            false);

                        usersThatMatched.Add(user);
                    };
                    reader.Close();
                }
            }


            foreach (DirectLookupUserModel userThatMatched in usersThatMatched)
            {
                UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(userThatMatched.UserName);
                userThatMatched.IsOpenIdLoggedInUser = profile.IsOpenIdLoggedInUser;
            }

            return usersThatMatched.First();
 
        }

        #endregion
    }
}